package com.zjc.yohoshop.mvp.persenter;

import android.util.Log;

import com.zjc.lib_core.mvp.presenter.BasePresenter;
import com.zjc.yohoshop.bean.BaseEntity;
import com.zjc.yohoshop.bean.CategoryEntity;
import com.zjc.yohoshop.bean.CategotyHotEntity;
import com.zjc.yohoshop.mvp.contract.CategoryFragmentContract;
import com.zjc.yohoshop.mvp.contract.HomeContract;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class CategoryFragmentPresenter extends BasePresenter<CategoryFragmentContract.CategoryFragmentIView,CategoryFragmentContract.CategoryFragmentIModel> {
    private int page=1;
    @Inject
    public CategoryFragmentPresenter(CategoryFragmentContract.CategoryFragmentIView categoryFragmentIView, CategoryFragmentContract.CategoryFragmentIModel categoryFragmentIModel) {
        super(categoryFragmentIView, categoryFragmentIModel);
    }

    //发起全部网络请求
    public void requestAll(){
        Map<String,String> map = new HashMap<>();
        JSONObject right = new JSONObject();
        try {
            right.put("categoryid","1");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String request = right.toString();
        m.requestCategoryRight(request).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }
                    @Override
                    public void onNext(BaseEntity baseEntity) {
                        Log.d("onNext","请求成功");
                        if (baseEntity instanceof CategotyHotEntity){
                            v.categoryRightSuccess((CategotyHotEntity) baseEntity);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("TT","onError"+e.toString());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
